﻿CREATE PROCEDURE [dbo].[CheckCashRegisterShift] (
	@CashRegisterShiftId int,
	@CashRegisterId int OUTPUT
)--проверка кассовой смены
AS
BEGIN
DECLARE @strMsg nvarchar(500), @intRet int, @intErrCode int
DECLARE @datCloseDT datetime, @datOpenDT datetime

SET NOCOUNT ON

IF (@CashRegisterShiftId Is Null)
BEGIN
	SELECT @intErrCode = 501, @strMsg = 'Отсутствует код кассовой смены!'
	RAISERROR(@strMsg, 11, 1)
	RETURN @intErrCode
END

SELECT @datOpenDT = OpenDT , @datCloseDT = CloseDT, @CashRegisterId = CashRegisterId
FROM [CD].[CashRegisterShifts] WHERE Id = @CashRegisterShiftId
IF (@@rowcount = 0)
BEGIN
	SELECT @intErrCode = 502, @strMsg = 'Не найдена кассовая смена с идентификатором ' + cast(@CashRegisterShiftId as varchar(10)) + ' !'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

IF @datCloseDT IS NOT NULL
BEGIN
	SELECT @intErrCode = 503, @strMsg = 'Кассовая смена с идентификатором ' + cast(@CashRegisterShiftId as varchar(10)) + ' закрыта !'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

IF datediff(hh, @datOpenDT, getdate()) > 24
BEGIN
	SELECT @intErrCode = 504, @strMsg = 'Кассовая смена с идентификатором ' + cast(@CashRegisterShiftId as varchar(10)) + ' открыта более 24 часов!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

END



